
global results ../china/disclosure
global china   ../china/data

import delimited using $results/beta_summary.csv, clear
ren v1 beta_p
ren v2 beta_x
ren v3 beta_c
ren v4 xi
merge 1:1 _n using $china/trade_share, keepusing(hs)
drop if _m!=3
drop _m
save $results/beta_summary, replace


use /projects/programs/monarch/HSConcord, clear
drop IO_des hs_des
gen hs_length=length(hs)
replace hs="0"+hs if hs_length==9
drop hs_length
merge m:1 hs using $china/hs1997
keep if _m==3
drop _m
ren IOCode industry_code
merge m:1 industry_code using $china/contract_intensity_IO_1997, keepusing(frac_lib_diff frac_lib_not_homog)
keep if _m==3
drop _m
duplicates tag hs, gen(dup)
drop if dup!=0
drop dup
save $china/nunn_hs, replace

use $china/hs_naics7_imports_07, clear
gen hs_length=length(hs)
replace hs="0"+hs if hs_length==9
drop hs_length
keep hs naicsbaseroot
ren naicsbaseroot naics
destring naics, replace force
merge m:1 naics using $china/naics_intensities
drop if _m!=3
drop _m
merge m:1 hs using $china/hs2007
drop if _m!=3
drop _m
duplicates drop hs, force
save $china/hs_intensities, replace


use $results/beta_summary, clear
replace beta_c=. if beta_c<-5
merge 1:1 hs using /projects/programs/monarch/bw_elast
drop if _m==2
drop _m

merge 1:1 hs using $china/nunn_hs, keepusing(frac_lib_diff frac_lib_not_homog)
drop if _m==2
drop _m

merge 1:1 hs using $china/hs_intensities, keepusing(cap_intensity skill_intensity)
drop if _m==2
drop _m

corr frac_lib_diff frac_lib_not_homog beta_x beta_c beta_p xi
corr sigma beta_x beta_c beta_p xi
corr cap_intensity skill_intensity beta_x beta_c beta_p xi

gen beta_total=beta_x + beta_c
corr frac_lib_diff frac_lib_not_homog beta_total
corr sigma beta_total
corr cap_intensity skill_intensity beta_total beta_x beta_c


use $results/beta_summary, clear
merge 1:1 hs using $china/trade_share, keepusing(trade_share)
replace beta_c=. if beta_c<-5
gen hs2=substr(hs,1,2)
destring hs2, replace force
drop if hs2>=90
drop if hs2<=5
drop if hs2>=68 & hs2<=71
drop if hs2>=25 & hs2<=27
drop if hs2>=72 & hs2<=83
drop if hs2>=44 & hs2<=49
drop if hs2>=28 & hs2<=38
summ beta_p beta_x beta_c xi
egen total_trade=total(trade_share)
replace beta_c=0 if beta_c==.
gen beta_p_weighted_component=beta_p*trade_share/total_trade
gen beta_x_weighted_component=beta_x*trade_share/total_trade
gen beta_c_weighted_component=beta_c*trade_share/total_trade
gen xi_weighted_component=xi*trade_share/total_trade

egen weighted_beta_p=total(beta_p_weighted_component)
egen weighted_beta_x=total(beta_x_weighted_component)
egen weighted_beta_c=total(beta_c_weighted_component)
egen weighted_xi=total(xi_weighted_component)
summ weighted_beta_p weighted_beta_x weighted_beta_c weighted_xi
